<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**验证函数
 * @param array $data
 * @param array $array
 * @return array
 */
function my_yanzheng($data=[],$array=[]){
    $datas = \think\Validate::make($array);
    if(!$datas->check($data)){
        $val['code'] = false;
        $val['msg'] = $datas->getError();
    }else{
        $val['code'] = true;
    }
    return $val;
}
//普通json返回数据
function json_my_response($msg='',$code=205,$data=null){
    return json(['code'=>$code,'msg'=>$msg,'data'=>$data]);
}

/**返回json给layui的分页
 * @param $data
 * @return \think\response\Json
 */
function json_page_response($data){
    return json(['code'=>0,'msg'=>'获取成功','count'=>$data['total'],'data'=>$data['data']]);
}

/**加密
 * @param string要加密的字符串 $str
 * @return string
 */
function my_encryption($str='',$haxi=false){
    if($haxi){
        return password_hash($str,PASSWORD_DEFAULT);
    }else{
        return md5(md5($str));
    }
}

/**判断加密是否一致
 * @param 未加密字符串 $str
 * @param 已加密字符串 $passworder
 * @param bool $haxi
 * @return bool
 */
function my_decryption($str,$passworder,$haxi=false){
    if($haxi){
        return password_verify($str,$passworder);
    }else{
        if(md5(md5($str))==$passworder){
            return true;
        }else{
            return false;
        }
    }
}
function getStrToFilePath($str){
    return rtrim(config('webroot'),'/').'/'.ltrim($str,'/');
}
/**判断是否是在微信打开
 * @return bool
 */
function is_weixin(){

    if ( strpos($_SERVER['HTTP_USER_AGENT'],

            'MicroMessenger') !== false ) {

        return true;

    }

    return false;
}

/**获取附件路径
 * @param $id
 * @param null $dizi
 * @param bool $yong
 * @return array|mixed|string
 * @throws \think\Exception
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 * @throws \think\exception\PDOException
 */
function getFilePath($id,$yong=true,$status=true){
    $data = request()->domain().rtrim(config('webroot'),'/').'/static/home/img/code.png';
    if(!$id){
        return $data;
    }
    $id = explode(',',$id);
//    return $id;
    if($id){
        $src = [];
        foreach ($id as $value){
            if(request()->module()=='admin'){
                $srcs = \think\Db::name(session('modular').'_enclosure')->where('id',$value)->find();
            }else{
                $srcs = \think\Db::name(request()->module().'_enclosure')->where('id',$value)->find();
            }
//            return $srcs;
            if($srcs){
                if($status){
                    if(request()->module()=='admin'){
                        \think\Db::name(session('modular').'_enclosure')->where('id',$value)->update(['status'=>1]);
                    }else{
                        \think\Db::name(request()->module().'_enclosure')->where('id',$value)->update(['status'=>1]);
                    }

                }
                $url = str_replace("\\","/",$srcs['path']);
                if(preg_match('/^\/uploads/',$url)){
                    $url = request()->domain().rtrim(config('webroot'),'/'). $url;
                }
                $src[] = $url;
            }else{
                $src[] = request()->domain().rtrim(config('webroot'),'/').'/static/home/img/code.png';
            }
        }
        if($yong){
            if(count($src)==1){
                return $src[0];
            }else{
                return $src;
            }
        }else{
            return $src;
        }
    }else{
        return $data;
    }
}
/**获取配置值
 * @param null $str
 * @return mixed|string
 */
function getSysTem($str=null,$vaue=true,$tupian=false){
    if(!$str){
        return '';
    }
    if(request()->module()=='admin'){
        if(session('modular')){
            $data =  \think\Db::name(session('modular').'_systems')->where('name|en_name',$str)->find();
        }else{
            return '';
        }
    }else{
        $data = \think\Db::name(request()->module().'_systems')->where('name|en_name',$str)->find();
    }
    if($data){
        if($vaue){
            return $data['value'];
        }
        if($tupian){
            return getFilePath($data['tupian']);
        }
        return $data;
    }else{
        return '';
    }
}
/**判断是否是手机
 * @return bool
 */
function is_mobile()
{
    $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    $is_pc = (strpos($agent, 'windows nt')) ? true : false;
    $is_mac = (strpos($agent, 'mac os')) ? true : false;
    $is_iphone = (strpos($agent, 'iphone')) ? true : false;
    $is_android = (strpos($agent, 'android')) ? true : false;
    $is_ipad = (strpos($agent, 'ipad')) ? true : false;

    if($is_iphone){
        return  true;
    }
    if($is_android){
        return  true;
    }
    if($is_ipad){
        return  true;
    }
    if($is_pc){
        return  false;
    }
    if($is_mac){
        return  false;
    }
    return false;
}
function getKeyWorder(\think\Request $request){
    $controller = $request->controller();
    $action = $request->action();
    $str = strtolower($controller.'/'.$action);
    $column = \think\Db::name($request->module().'_column')
        ->where('index_action','like',"%$str%")
        ->select();
    $keywords = '';
    $description = '';
    if($column){
        foreach ($column as $values){
            $shuju = \think\Db::name($request->module().'_keywords')
                ->where('status',1)
                ->where('lanmu',$values['id'])
                ->select();
            foreach ($shuju as $value){
                $keywords .= $value['keyword'].',';
                $description .= $value['descriptions'].',';
            }
        }
    }
    $newshuju = \think\Db::name($request->module().'_keywords')
        ->where('status',1)
        ->where('lanmu',0)
        ->select();
    foreach ($newshuju as $value){
        $keywords .= $value['keyword'].',';
        $description .= $value['descriptions'].',';
    }
    $keywords = rtrim($keywords,',');
    $description = rtrim($description,',');
    return ['keywords'=>$keywords,'description'=>$description];
}